package com.example.springmvcdemo4.Controller;

import com.example.springmvcdemo4.Service.MessageService;
import com.example.springmvcdemo4.model.MessageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/message")
@RestController
public class MessageController {

    @Autowired
    private MessageService messageService;
    @RequestMapping("/publish")
    public boolean publish(MessageInfo messageInfo){
        //1、参数校验
        //2、存储数据    暂时存储在内存中
        if(!StringUtils.hasLength(messageInfo.getFrom())
                ||!StringUtils.hasLength(messageInfo.getTo())
                ||!StringUtils.hasLength(messageInfo.getMessage())){
            return false;
        }
//        messageInfos.add(messageInfo);
        int result = messageService.publishMessage(messageInfo);
        if (result > 0){
            return true;
        }
        return  false;
    }
    //获取留言信息
    @RequestMapping("/getList")
    public List<MessageInfo> getList(){
        return messageService.getList();
    }
}
